package com.o2o.app.service;

/**
 * 驿站女孩
 */
import java.util.ArrayList;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.o2o.app.R;

public class ServiceGirlActivity extends Activity {
	private static final int CLICK_CALL_PHONE = -1; // 联系电话
	private static final int CLICK_CALL_BPHONE = -2; // 联系电话
	public static ServiceGirlActivity SGA;
	// 联系电话按钮
	private ArrayList<RelativeLayout> phoneLists = new ArrayList<RelativeLayout>();

	private ArrayList<Button> phoneList1 = new ArrayList<Button>();
	/**
	 * 返回
	 */
	private Button btn_back;

	/**
	 * 驿站女孩详情布局
	 */
	private LinearLayout layout;

	// 测试数组最大数
	private int i_max = 2;

	/**
	 * 服务建议按钮
	 */
	private RelativeLayout rlt_shuru;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

		setContentView(R.layout.service_girl);

		// 判断是从个人中心进入还是从服务中进入
		if (!TextUtils.isEmpty(getIntent().getStringExtra("forhome"))
				&& "forhome".equals(getIntent().getStringExtra("forhome"))) {
			Button btn_home = (Button) findViewById(R.id.btn_home);
			btn_home.setVisibility(View.VISIBLE);
			btn_home.setOnClickListener(new ClickEvent());
		}

		initViews();

		showView();
	}

	private void initViews() {
		btn_back = (Button) findViewById(R.id.btn_back);
		btn_back.setOnClickListener(new ClickEvent());

		rlt_shuru = (RelativeLayout) findViewById(R.id.rlt_shuru);
		rlt_shuru.setOnClickListener(new ClickEvent());

	}

	/**
	 * 显示驿站女孩详情
	 */
	private void showView() {
		layout = (LinearLayout) findViewById(R.id.lly_all_content01);
		// layout.removeAllViews();
		layout.setVisibility(View.VISIBLE);

		for (int i = 0; i < i_max; i++) {
			LinearLayout child = (LinearLayout) LayoutInflater.from(
					ServiceGirlActivity.this).inflate(
					R.layout.service_girl_item, null);
			child.setId(i);

			RelativeLayout rlt_demo = (RelativeLayout) child
					.findViewById(R.id.rlt_phone);
			rlt_demo.setId(CLICK_CALL_PHONE);
			rlt_demo.setOnClickListener(new ClickEvent());
			phoneLists.add(rlt_demo);

			Button btn_phone = (Button) child.findViewById(R.id.btn_phone);
			btn_phone.setId(CLICK_CALL_BPHONE);
			btn_phone.setOnClickListener(new ClickEvent());
			phoneList1.add(btn_phone);

			layout.addView(child);

		}
	}

	/**
	 * 点击事件
	 * 
	 * @author Administrator
	 * 
	 */
	class ClickEvent implements View.OnClickListener {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			switch (v.getId()) {
			case R.id.btn_back: { // 返回
				finish();
			}
				break;
			case CLICK_CALL_PHONE: {
				checkPhoneItem(v, 1);
			}
				break;
			case CLICK_CALL_BPHONE: {
				checkPhoneItem(v, 2);
			}
				break;
			case R.id.rlt_shuru: { // 服务建议按钮
				// TODO
			}
				break;
			case R.id.btn_home: { // 返回首页
				finish();
			}
				break;
			default:
				break;
			}
		}

	}

	/**
	 * 点赞
	 */
	private void checkPhoneItem(View v, int type) {
		switch (type) {
		case 1: {
			int index = -1;
			for (int i = 0; i < i_max; i++) { // 遍历选择了哪项
				if (phoneLists.size() > 0 && phoneLists.get(i).equals(v)) { // 为真、确定是选了哪项
					index = i;
				}
			}

			if (index >= 0) {
				callPhone();
			}
		}

			break;
		case 2: {
			int index = -1;
			for (int i = 0; i < i_max; i++) { // 遍历选择了哪项
				if (phoneList1.size() > 0 && phoneList1.get(i).equals(v)) { // 为真、确定是选了哪项
					index = i;
				}
			}

			if (index >= 0) {
				callPhone();
			}
		}
			break;
		default:
			break;
		}

	}

	private void callPhone() {
		AlertDialog.Builder builder = new Builder(ServiceGirlActivity.this);
		builder.setMessage("确定要电话给驿站女孩吗");
		builder.setTitle("提示");
		builder.setPositiveButton("确认",
				new android.content.DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {

						Intent intent = new Intent(Intent.ACTION_CALL, Uri
								.parse("tel:" + "18612615235"));
						startActivity(intent);
					}
				});
		builder.setNegativeButton("取消",
				new android.content.DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();
					}
				});
		builder.create().show();
	}
}
